/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package base.Items;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import java.io.IOException;
import java.util.TreeMap;

/**
 *
 * @author Paulo
 */
public class ItemManager implements Savable {
    private TreeMap<String, Item> inventory;
    
    private Equipment rightHand;
    private Equipment leftHand;
    private Equipment chest;
    private Equipment ring;
    private Equipment gloves;
    private Equipment head;
    private Equipment necklace;
    private Equipment legs;
    
    public ItemManager() {
        this.inventory = new TreeMap<String, Item>();
    }
    
    public void addItem(Item item) {
        if (inventory.containsKey(item.getName())) {
            Item invItem = inventory.get(item.getName());
            invItem.setStack(invItem.getStack() + item.getStack());
        } else {
            inventory.put(item.getName(), item);
        }
    }
    
    public void removeItem(Item item) {
        this.inventory.remove(item.getName());
    }
    
    public Equipment getRightHand() {
        return this.rightHand;
    }
    public void setRightHand(Equipment item) {
        this.rightHand = item;
    }
    
    public Equipment getLeftHand() {
        return this.leftHand;
    }
    public void setLeftHand(Equipment item) {
        this.leftHand = item;
    }
    
    public Equipment getChest() {
        return this.chest;
    }
    public void setChest(Equipment item) {
        this.chest = item;
    }
    
    public Equipment getRing() {
        return this.ring;
    }
    public void setRing(Equipment item) {
        this.chest = item;
    }
    
    public Equipment getGloves() {
        return this.gloves;
    }
    public void setGloves(Equipment item) {
        this.chest = item;
    }
    
    public Equipment getHead() {
        return this.head;
    }
    public void setHead(Equipment item) {
        this.chest = item;
    }
    
    public Equipment getNecklace() {
        return this.necklace;
    }
    public void setNecklace(Equipment item) {
        this.chest = item;
    }
    
    public Equipment getLegs() {
        return this.legs;
    }
    public void setLegs(Equipment item) {
        this.chest = item;
    }
    
    public void write(JmeExporter ex) throws IOException {
        OutputCapsule out = ex.getCapsule(this);
        
        Item[] array = new Item[0];
        array = this.inventory.values().toArray(array);
        
        out.write(array, "inventory", null);
        out.write(rightHand, "rightHand", null);
        out.write(leftHand, "leftHand", null);
        out.write(chest, "chest", null);
        out.write(ring, "ring", null);
        out.write(gloves, "gloves", null);
        out.write(head, "head", null);
        out.write(necklace, "necklace", null);
        out.write(legs, "legs", null);
    }

    public void read(JmeImporter im) throws IOException {
        InputCapsule in = im.getCapsule(this);
        
        Item[] array = (Item[]) in.readSavableArray("inventory", new Item[0]);
        
        inventory = new TreeMap<String, Item>();
        for (Item i : array) {
            inventory.put(i.getName(), i);
        }
        
        rightHand = (Equipment) in.readSavable("rightHand", null);
        leftHand = (Equipment) in.readSavable("leftHand", null);
        chest = (Equipment) in.readSavable("chest", null);
        ring = (Equipment) in.readSavable("ring", null);
        gloves = (Equipment) in.readSavable("gloves", null);
        head = (Equipment) in.readSavable("head", null);
        necklace = (Equipment) in.readSavable("necklace", null);
        legs = (Equipment) in.readSavable("legs", null);
    }
}
